package com.party.admin.biz.dynamic;

import com.party.admin.biz.file.FileBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.output.dynamic.DynamicOutput;
import com.party.core.model.circle.CircleTopic;
import com.party.core.model.dynamic.Dynamic;
import com.party.core.model.dynamic.DynamicType;
import com.party.core.model.dynamic.Dypics;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.circle.biz.CircleTopicBizService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.dynamic.IDynamicService;
import com.party.core.service.dynamic.IDypicsService;
import com.party.core.service.dynamic.biz.DynamicBaseBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 动态业务接口
 * Created by wei.li
 *
 * @date 2017/3/9 0009
 * @time 14:53
 */
@Service
public class DynamicBizService {

    @Autowired
    private IDypicsService dypicsService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IDynamicService dynamicService;

    @Autowired
    private FileBizService fileBizService;

    @Autowired
    private DynamicBaseBizService dynamicBaseBizService;

    @Autowired
    private CircleTopicBizService circleTopicBizService;

    @Autowired
    private CosBizService cosBizService;

    @Autowired
    private IMemberGroupService memberGroupService;

    /**
     * 转换数据
     *
     * @param input 动态信息
     * @return 动态输出视图
     */
    public DynamicOutput transformCommon(Dynamic input){
        DynamicOutput output = DynamicOutput.transform(input);

        // 动态图片列表
        List<Dypics> dypics = dypicsService.findByDynamicId(input.getId(), null);
        output.setPicList(dypics);

        // 点赞数
        output.setCommentNum(input.getCommentNum());

        // 评论数
        output.setLoveNum(input.getLoveNum());

        // 详情二维码
        String filePath = fileBizService.getUploadCIPath("dynamic", input.getCreateBy());
        String qrCodeUrl = cosBizService.getPreviewPictureUrl(filePath + input.getId());
        output.setQrCodeUrl(qrCodeUrl);

        // 动态发布者
        Member authorMember = memberService.get(input.getCreateBy());
        if (null == authorMember) {
            MemberGroup memberGroup = memberGroupService.get(input.getCreateBy());
            if (null != memberGroup) {
                output.setMemberName(memberGroup.getRealname());
                output.setMemberLogo(memberGroup.getLogo());
            }
        } else {
            output.setMemberLogo(authorMember.getLogo());
            output.setMemberName(authorMember.getRealname());
        }
        return output;
    }

    /**
     * 编辑或保存话题
     *
     * @param entity 动态信息
     * @throws Exception 异常信息
     */
    @Transactional
    public void saveBizTopic(Dynamic entity, CircleTopic topic) throws Exception {
        entity.setDynamicType(DynamicType.CIRCLE.getCode());
        String dynamicId = dynamicBaseBizService.saveBizDynamic(entity, RealmUtils.getCurrentUser().getId());
        topic.setDynamic(dynamicId);
        circleTopicBizService.saveTopic(topic,RealmUtils.getCurrentUser().getId());
        try {
            Dynamic dynamic = dynamicService.get(dynamicId);
            String filePath = fileBizService.getUploadCIPath("dynamic", dynamic.getCreateBy());
            String fileName = dynamic.getId();
            String content = "community/dynamic_detail.html?id=" + dynamic.getId();
            fileBizService.uploadCloudOut(fileName,filePath,content,dynamic.getCreateBy());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
